package com.nokia.examplegame;

import javax.microedition.lcdui.*;
import javax.microedition.lcdui.game.LayerManager;

public class GameLayerManager extends LayerManager {

	private PlayerSprite player;

	private ObstacleSprite[] leftObstacles;

	private ObstacleSprite[] rightObstacles;

	private BackgroundTiledLayer background;

	private int cornerX;

	private int cornerY;

	private int displayHeight;

	private int displayWidth;

	private int currentLeftX;

	private boolean isMovingLeft;

	private Image playerImage;

	private Image obstacleImage;

	private Image backgroundImage;

	public GameLayerManager(int cornerX, int cornerY, int width, int height,
			Image playerImage, Image obstacleImage, Image backgroundImage) {
		this.playerImage = playerImage;
		this.obstacleImage = obstacleImage;
		this.backgroundImage = backgroundImage;
		this.cornerX = cornerX;
		this.cornerY = cornerY;
		this.displayHeight = height;
		this.displayWidth = width;
		this.currentLeftX = (BackgroundTiledLayer.TILE_WIDTH)
				* (BackgroundTiledLayer.TILE_REPEAT);
		setViewWindow(0, 0, width, height);
	}

	public void paint(Graphics g) throws Exception {
		if (player == null) {
			player = new PlayerSprite(playerImage, currentLeftX + displayWidth
					/ 2, displayHeight - PlayerSprite.HEIGHT - 2);
			append(player);
		}

		if (leftObstacles == null) {
			leftObstacles = new ObstacleSprite[2];
			for (int i = 0; i < leftObstacles.length; i++) {
				leftObstacles[i] = new ObstacleSprite(obstacleImage, true,
						displayHeight);
				append(leftObstacles[i]);
			}
		}
		if (rightObstacles == null) {
			rightObstacles = new ObstacleSprite[2];
			for (int i = 0; i < rightObstacles.length; i++) {
				rightObstacles[i] = new ObstacleSprite(obstacleImage, false,
						displayHeight);
				append(rightObstacles[i]);
			}
		}
		if (background == null) {
			background = new BackgroundTiledLayer(backgroundImage,
					displayWidth, displayHeight);
			append(background);
		}
		setViewWindow(currentLeftX, 0, displayHeight, displayWidth);
		paint(g, cornerX, cornerY);
	}

	public int updateLayers(int gameTicks) {
		if (isMovingLeft) {
			currentLeftX--;
		} else {
			currentLeftX++;
		}
		player.update(gameTicks, isMovingLeft);

		int pointsToAdd = 0;
		int pointsToMinus = 0;

		for (int i = 0; i < leftObstacles.length; i++) {
			ObstacleSprite obstacle = leftObstacles[i];

			pointsToAdd += obstacle.update(player, gameTicks, currentLeftX,
					currentLeftX + displayWidth);
			pointsToMinus += player.checkCollision(obstacle);
		}
		for (int i = 0; i < rightObstacles.length; i++) {
			ObstacleSprite obstacle = rightObstacles[i];
			pointsToAdd += obstacle.update(player, gameTicks, currentLeftX,
					currentLeftX + displayWidth);

			pointsToMinus += player.checkCollision(obstacle);
		}
		wrap();

		return pointsToAdd - pointsToMinus;
	}

	void wrap() {
		int backgroundTileWidth = BackgroundTiledLayer.TILE_WIDTH;
		int backgroundTileRepeat = BackgroundTiledLayer.TILE_REPEAT;
		int pos = backgroundTileWidth * backgroundTileRepeat;

		if (currentLeftX % (pos) == 0) {
			if (isMovingLeft) {
				player.move(pos, 0);
				currentLeftX += pos;
				for (int i = 0; i < leftObstacles.length; i++) {
					leftObstacles[i].move(pos, 0);
				}
				for (int i = 0; i < rightObstacles.length; i++) {
					rightObstacles[i].move(pos, 0);
				}

				background.move(pos, 0);
			} else {
				player.move(-(pos), 0);
				currentLeftX -= pos;
				for (int i = 0; i < leftObstacles.length; i++) {
					leftObstacles[i].move(-pos, 0);
				}
				for (int i = 0; i < rightObstacles.length; i++) {
					rightObstacles[i].move(-pos, 0);
				}
				background.move(-pos, 0);
			}
		}
	}

	public void setPlayerMovingLeft(boolean movingLeft) {
		this.isMovingLeft = movingLeft;

	}

	public void setPlayerJumping(boolean b) {
		player.jump();
	}

}